package counter;

import main.Main;


/**
 * @author Yoni Seroussi
 * @author Roei Raviv
 *
 */
public class SyncCounterThread extends Thread {

	private CounterWithBarrier _syncedCounter;
	
	/**
	 * C'tor
	 * @param counter The shared synchronized counter obj.
	 */
	public SyncCounterThread(CounterWithBarrier syncedCounter) {
		_syncedCounter = syncedCounter;
	}

	@Override
	public void run() {
		int cachedVal;
		int countTo = Main.getCountTo();
		for (int iterNum = 0; iterNum < countTo ; iterNum++) {
		  cachedVal = _syncedCounter.getValue();
			_syncedCounter.await(); // all threads read the same value.
			cachedVal++;
			_syncedCounter.setValue(cachedVal);
		}
	}
}
